package com.up.stores.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.up.stores.entity.Admin;
import com.up.stores.entity.Request;
import com.up.stores.entity.User;
import com.up.stores.mapper.UserMapper;
import com.up.stores.service.IBaseAdminService;
import com.up.stores.service.IRequestService;
import com.up.stores.service.ex.UpdateException;
import com.up.stores.until.JsonResult;
import com.up.stores.vo.RequestDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags="申请模块")
@RestController
@RequestMapping("request")
public class RequestController extends BaseController{
    @Autowired
    private IRequestService requestService;

    @Autowired
    private IBaseAdminService baseAdminService;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value="提交申请")
    @PostMapping("submit")
    public JsonResult<Request> submit(String adm_name,
                                      //String reason,
                                      HttpSession session) {
        //new一个req对象并进行赋值
        Request req = new Request();
        req.setAdmName(adm_name);
        req.setNorName(getUsernameFromSession(session));
        //req.setReason(reason);
        //调用业务对象执行添加申请
        //是否需要判断某个用户已有申请而禁止其再申请？
        requestService.submitReq(req);
        return new JsonResult<Request>(OK, req);
    }

    @ApiOperation(value="撤销申请")
    @DeleteMapping("revoke")
    public JsonResult<Void> revoke(@RequestBody Integer reqId) {
        //调用业务对象执行审批
        requestService.revokeReq(reqId);
        return new JsonResult<Void>(OK);
    }

    @ApiOperation(value="审批申请")
    @PutMapping("examine")
    public JsonResult<Void> examine(Integer reqId,
                                     String norName,
                                     Integer reqStatus,
                                     HttpSession session) {
        //调用业务对象执行审批
        requestService.approvalReq(reqId, reqStatus, getUsernameFromSession(session), new Date());
        //如果同意当前申请，则需要将user表中的admin_id字段值设置为当前的adminId值，将lib_id的值设置为当前管理员的lib_id的值
        if(reqStatus == 2){
            Admin admin = baseAdminService.getByUid(getuidFromSession(session));
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_name", norName)
                    .set("adm_id", getuidFromSession(session))
                         .set("lib_id", admin.getLibId());
            Integer rows = userMapper.update(null, updateWrapper);
            if (rows < 1) {
                // 是：抛出UpdateException
                throw new UpdateException("通过申请时发生异常");
            }
        }
        return new JsonResult<Void>(OK);
    }

    //这里的返回的是一个列表
    @ApiOperation(value="显示当前管理员的申请", notes="此处根据申请的状态展示某一状态（待审批-0，驳回-1，通过-2）的申请")
    @GetMapping("display")
    public JsonResult<List<RequestDTO>> display(Integer reqStatus,
                                     HttpSession session) {
        //调用业务对象执行展示
        List<RequestDTO> list = requestService.findByAdmNameAndReqStatus(getUsernameFromSession(session), reqStatus);
        return new JsonResult<>(OK, list);
    }

//    @ApiOperation(value="判断当前用户是否已经提交过一次申请", notes="若已提交过一个申请则禁止申请功能")
//    @GetMapping("judge")
//    public JsonResult<Boolean> judge(HttpSession session) {
//        //调用业务对象执行判断
//        Boolean flag = requestService.judgeReq(getuidFromSession(session));
//        return new JsonResult<Boolean>(OK, flag);
//    }

    @ApiOperation(value="管理员删除某归属其的普通用户")
    @GetMapping("deleteUser")
    public JsonResult<Void> deleteUser(String norName) {
        //调用业务对象执行判断
        requestService.deleteUser(norName);
        return new JsonResult<Void>(OK);
    }
}
